﻿using System.Collections.Generic;
using KillBill.Business.Entities;
using KillBill.Presentation.Common.WindowsForms.View;
using KillBill.Presentation.Modules.Suppliers.Presenter.IView;
using KillBill.Presentation.Modules.Suppliers.Presenter;
using System.Linq;

namespace KillBill.Presentation.Modules.Suppliers.View
{
    public partial class ProviderListView : KillBillWinFormView, IProviderList
    {
        public ProviderListPresenter Presenter { get; set; }

        private List<Provider> _providerList;

        public ProviderListView()
        {
            InitializeComponent();
            Presenter = new ProviderListPresenter(this);
        }

        public List<Provider> Providers
        {
            set
            {
                var rowIndex = 0;
                DgvProviders.Rows.Clear();
                foreach (var provider in value)
                {
                    DgvProviders.Rows.Add();
                    DgvProviders[0, rowIndex].Value = provider.ProviderRUC;
                    DgvProviders[1, rowIndex].Value = provider.Name;
                    DgvProviders[2, rowIndex].Value = provider.Person.LastName + " " + provider.Person.FirstName;                
                    //DgvProviders[3, rowIndex].Value = (provider.IsActive ? Properties.Resources.Active : Properties.Resources.Delete);
                    DgvProviders[4, rowIndex].Value = provider.ModifiedDate; 
                    rowIndex++;
                }
                _providerList = value;
            }
        }

        private void BtnRefresh_Click(object sender, System.EventArgs e)
        {
            Presenter.Find(TxtSearch.Text);
        }

        private void TxtSearch_TextChanged(object sender, System.EventArgs e)
        {
            Presenter.Find(TxtSearch.Text);
        }

        private void BtnEdit_Click(object sender, System.EventArgs e)
        {
            var provider =
                _providerList.Where(p => p.ProviderRUC == DgvProviders.SelectedRows[0].Cells[0].Value.ToString()).First();
            var form = new ProviderAddEditView(provider);
            form.Show();
        }

        private void BtnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            var f = new ProviderAddEditView();
            f.Show();
        }
    }
}
